#include "hc_tree_view_model.h"

void TreeModel::InitRootNode(TreeNode *rootNode) {
  if (rootNode == nullptr) {
    return;
  }
  rootNode->parent_ = rootNode_;
  rootNode_->addChild(rootNode);
}

void TreeModel::AddTree(const TreeClickItem &parentItem,
                        const TreeClickItem &item) {
  // 如果该节点的父节点尚未存在，先挂到根节点并存入 pendingChildren_
  TreeNode *node = findNodeByItem(rootNode_, item);
  if (node != nullptr) {
    return;
  }
  TreeNode *parentNode = findNodeByItem(rootNode_, parentItem);
  if (node == nullptr) {
    return;
  }

  TreeNode *newNode = new TreeNode(item, parentNode);
  parentNode->addChild(newNode);

  QModelIndex newParentIndex = createIndex(newNode->row(), 0, newNode);
  int newRow = parentNode->childCount() - 1;
  beginInsertRows(newParentIndex, newRow, newRow);
  endInsertRows();
}

void TreeModel::MoveItem(const TreeClickItem &item) {
  TreeNode *nodeToRemove = findNodeByItem(rootNode_, item);
  if (!nodeToRemove) return;

  TreeNode *parentNode = nodeToRemove->parent_;
  int row = nodeToRemove->row();

  beginRemoveRows(createIndex(parentNode->row(), 0, parentNode), row, row);
  parentNode->removeChild(nodeToRemove);
  delete nodeToRemove;
  endRemoveRows();
}

void TreeModel::EditItem(const TreeClickItem &item) {
  TreeNode *node = findNodeByItem(rootNode_, item);
  if (node == nullptr) {
    return;
  }
  node->data_ = item;
}

TreeNode *TreeModel::FindNodeByItem(const TreeClickItem &item) {
  return findNodeByItem(rootNode_, item);
}

TreeNode *TreeModel::findNodeByItem(TreeNode *node,
                                    const TreeClickItem &targetItem) {
  if (node->data_ == targetItem) {
    return node;
  }

  for (TreeNode *child : node->children_) {
    TreeNode *result = findNodeByItem(child, targetItem);
    if (result != nullptr) {
      return result;
    }
  }

  return nullptr;
}

QModelIndex TreeModel::index(int row, int column,
                             const QModelIndex &parent) const {
  if (!hasIndex(row, column, parent)) {
    return QModelIndex();
  }

  TreeNode *parentNode;
  if (!parent.isValid()) {
    parentNode = rootNode_;
  } else {
    parentNode = static_cast<TreeNode *>(parent.internalPointer());
  }

  TreeNode *childNode = parentNode->child(row);
  if (childNode) return createIndex(row, column, childNode);
  return QModelIndex();
}

QModelIndex TreeModel::parent(const QModelIndex &index) const {
  if (!index.isValid()) {
    return QModelIndex();
  }

  TreeNode *childNode = static_cast<TreeNode *>(index.internalPointer());
  TreeNode *parentNode = childNode->parent_;
  if (childNode == nullptr || parentNode == nullptr) {
    // 问题
    return QModelIndex();
  }

  if (parentNode == rootNode_) {
    return QModelIndex();
  }

  return createIndex(parentNode->row(), 0, parentNode);
}

int TreeModel::rowCount(const QModelIndex &parent) const {
  TreeNode *parentNode;
  if (!parent.isValid()) {
    parentNode = rootNode_;
  } else {
    parentNode = static_cast<TreeNode *>(parent.internalPointer());
  }

  return parentNode->childCount();
}

int TreeModel::columnCount(const QModelIndex &parent) const {
  Q_UNUSED(parent)
  return 1;  // 简化为单列
}

QVariant TreeModel::data(const QModelIndex &index, int role) const {
  if (!index.isValid()) {
    return QVariant();
  }

  TreeNode *node = static_cast<TreeNode *>(index.internalPointer());
  if (node == nullptr) {
    return QVariant();
  }

  // 设置显示的图标
  if (role == Qt::DecorationRole) {
    // std::string type = node->data_.type_;
    // QIcon icon;
    // if (TreeItemIconCtrl::GetIns().GetIcon(type, icon)) {
    //   return icon; // 返回你想要的图标
    // }
  }

  // 设置显示的内容
  if (role == Qt::DisplayRole) {
    return node->data_.name_;
  }

  // 设置节点是否禁用
  if (role == Qt::ForegroundRole && node->isDisabled_) {
    return QBrush(Qt::gray);  // 如果节点被禁用，字体颜色变灰
  }

  // 设置自定义颜色
  if (role == Qt::ForegroundRole) {
    return QBrush(node->textColor_);
  }

  // 设置自定义字体
  if (role == Qt::FontRole) {
    return node->customFont_;
  }

  return QVariant();
}

bool TreeModel::setData(const QModelIndex &index, const QVariant &value,
                        int role) {
  if (!index.isValid()) {
    return false;
  }

  TreeNode *node = static_cast<TreeNode *>(index.internalPointer());
  if (node == nullptr) {
    return false;
  }

  // 根据角色设置数据
  if (role == Qt::EditRole) {
    node->data_.name_ = value.toString();
    emit dataChanged(index, index);
    return true;
  }

  return false;
}

Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
  if (!index.isValid()) {
    return Qt::NoItemFlags;
  }

  return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
}
